import os
import requests

class GlobalVar:
    def __init__(self):
        self.starter_pkgs = []
        self.pkg_queue = []
        self.finished_pkg = []
        self.miss_pkg = []
        self.finished_pkg_node = []
        self.finished_pkg_depend = []
        self.order_num = 0

    def save_list(self, pkg_list, filename):
        with open(filename, 'w', encoding="utf-8") as f:
            for value in pkg_list:
                f.write(str(value) + '\n')

    def load_list(self, filename):
        with open(filename, 'r', encoding="utf-8") as f:
            pkg_list = f.read().split('\n')
            return pkg_list

    def get_starter_pkgs(self):
        if not os.path.exists('./starter_pkgs.txt'):
            self.crawl_starter_pkgs()
        self.starter_pkgs = self.load_list('./starter_pkgs.txt')

    def crawl_starter_pkgs(self, st=1, ed=10):
        pkg_list = []
        for i in range(st, ed + 1):
            url = 'https://api.github.com/search/repositories?q=topic:NODEJS&language:JavaScript&sort=stars&page=%s&per_page=100' % i
            r = requests.get(url)
            response_dict = r.json()
            repo_dicts = response_dict['items']
            for repo_dict in repo_dicts:
                pkg_list.append(repo_dict['name'])
        print(f"get {len(pkg_list)} most popular projects from github, not all in npm")
        record = open("summary.txt", "a")
        record.write('-------------------------------------------------------\n')
        record.write(f"GET {len(pkg_list)} popular projects from github page {st} to {ed}, not all in npm")
        record.close()
        self.load_list('./starter_pkgs.txt')



globalVar = GlobalVar()